class InsFilterRule {
    constructor({
                    id = '',
                    insId = '',
                    subtype = '',
                    valueStart = '',
                    valueEnd = '',
                    keywordRange = '',
                    keywordRangeList = [],
                } = {}) {
        this.id = id
        this.insId = insId
        this.subtype = subtype
        this.valueStart = valueStart
        this.valueEnd = valueEnd
        this.keywordRange = keywordRange
        this.keywordRangeList = keywordRangeList
    }
}

//vue
const app = new BaseValidateVue({
    el: '#app',
    data: function () {
        return {
            opers: [],
            houses: [],
            options: [{
                value: '0',
                label: '正文标题及正文本身',
            }, {
                value: '1',
                label: '附件文件题目',
            }, {
                value: '2',
                label: '附件正文',
            }],
            formData: {
                id: '',
                reason: '',
                log: '0',
                report: '0',
                effectTime: '',
                expiredTime: '',
                idcId: '',
                houseId: '',
                owner: 'ISMS添加',
                visible: '1',
                operationType: '1',
                level: '100000000000',
                timeStamp: '',
                reportStatus: '0',
                rawPacket: 'ISMS添加',
                insSource: '2',
                insFilterRules: [
                    new InsFilterRule(),
                ],
            },
            houseIdList: [],
            nowTime: '',
            ruleList: [
                new ValidateObject({
                    ruleName: 'value_start_validate',
                    validate: this.valueStartValidate,
                    msg: '请正确输入域名',
                }),
            ],
        }
    },
    methods: {
        createValidateList() {
            return this.ruleList;
        },
        async initOpers() {
            this.opers = await baseOperCompanyApi.selectList();
        },
        async idcIdChange() {
            this.houses = await baseHouseInfoApi.selectListByParam({idcId: this.formData.idcId});
        },
        addInsRule() {
            this.formData.insFilterRules.push(new InsFilterRule({insId: this.formData.id}));
        },
        async submitForm() {
            //当前时间必须大于进入页面时间
            this.$validator.validateAll().then(async (res) => {
                if (res) {
                    if (rx.dateBetween(this.nowTime, this.formData.effectTime).milliSecond() < 0) {
                        await rxPrompt.msg('生效时间必须大于当前时间')
                        return;
                    }
                    if (rx.dateBetween(this.formData.effectTime, this.formData.expiredTime).milliSecond() < 0) {
                        await rxPrompt.msg('过期时间必须大于生效时间');
                        return;
                    }
                    //校验ip是否同一网段
                    if (this.ipValidate()) {
                        return;
                    }
                    var result = await insFilterApi.add(this.formData);
                    if (result.code === 200) {
                        await rxPrompt.msg('新增成功')
                        forward('/command-manage/admin_commandfilter')
                    } else {
                        await rxPrompt.msg(`新增失败,${result.msg}`)
                    }
                } else {
                    rxPrompt.msg("输入存在错误！");
                }
            })

        },
        changeKeywordRange(index) {
            var insFilterRule = this.formData.insFilterRules[index]
            insFilterRule.keywordRange = insFilterRule.keywordRangeList.join();
        },
        changeHouseId() {
            this.formData.houseId = this.houseIdList.join()
        },
        deleteInsRule(index) {
            this.formData.insFilterRules.splice(index, 1);
        },
        subtypeChange(index) {
            this.formData.insFilterRules[index].valueStart = '';
            this.formData.insFilterRules[index].valueEnd = '';
        },
        async valueStartValidate(val, args) {
            if (val === '') {
                return true;
            }
            const reg = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;
            return reg.test(val);
        },
        ipValidate() {
            var flag = false;
            this.formData.insFilterRules.forEach((val, index, arr) => {
                const endField = 'valueEnd' + index;
                const {valueEnd = '', valueStart = '', subtype = ''} = val;
                if (valueEnd == null || valueEnd === '' || (parseInt(subtype) !== 4 && parseInt(subtype) !== 4)) {
                    return;
                }
                if (valueEnd.substring(0, 11) !== valueStart.substring(0, 11)) {
                    console.log("不在同一网段");
                    this.errors.remove(endField);
                    this.errors.add({
                        field: endField,
                        msg: '起始值和结束值不在同一网段'
                    });
                    flag = true;
                }
            });
            return flag;
        }
    },
    watch: {},
    mounted: async function () {
        this.initOpers();
        this.nowTime = new Date();
    },

});
